package com.cyandpos.pad.utils.view;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cyandpos.pad.R;

/**
 * 多项单选选择弹窗
 */
public class SelectDialogCommon extends AlertDialog {
    private static  SelectDialogCommon selectDialogCommon;
    private Context context;
    private View view;
    private TextView tv_title;
    private RelativeLayout rl_close;
    private GridView gv_title;
    private Button bt_item;
    private SelectDialogOnItemClickListener selectDialogOnItemClickListener;

    private  SelectDialogCommon(Context context, SelectDialogOnItemClickListener selectDialogOnItemClickListener) {
        super(context);
        this.context=context;
        this.selectDialogOnItemClickListener=selectDialogOnItemClickListener;
        view = LayoutInflater.from(context).inflate(R.layout.dialog_more_pay,null);
        tv_title=view.findViewById(R.id.tv_title);
        rl_close=view.findViewById(R.id.rl_close);
        gv_title=view.findViewById(R.id.gv_pay_more);
        bt_item=view.findViewById(R.id.bt_item);
    }

      public static  SelectDialogCommon getSelectDialogCommon(Context context, SelectDialogOnItemClickListener selectDialogOnItemClickListener){
         if(selectDialogCommon==null){
             selectDialogCommon=new SelectDialogCommon(context,selectDialogOnItemClickListener);
         }
         return selectDialogCommon;
      }

    /**
     * 显示弹窗
     * @param titles
     * @param isClose
     */
    public  void showDialog(String[] titles,boolean isClose){
        selectDialogCommon.setCancelable(isClose);
        if(titles!=null&&titles.length>0) {
            final TitleAdapter titleAdapter = new TitleAdapter(context, titles);
            gv_title.setAdapter(titleAdapter);
        }
        gv_title.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectDialogCommon.dismiss();
                selectDialogCommon=null;
                if(selectDialogOnItemClickListener!=null){
                    selectDialogOnItemClickListener.selectDialogOnItemClick(position);
                }
            }
        });
        rl_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectDialogCommon.dismiss();
                selectDialogCommon=null;
            }
        });
        bt_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectDialogCommon.dismiss();
                selectDialogCommon=null;
            }
        });
        selectDialogCommon.setView(view,0,0,0,0);
        selectDialogCommon.show();
        selectDialogCommon.getWindow().setBackgroundDrawableResource(R.drawable.dialog_bg_common);
    }

    /**
     * 设置标题
     * @param title
     */
    public void setTitle(String title){
        if(tv_title!=null)
        tv_title.setText(title);
    }
    public class TitleAdapter extends BaseAdapter{
        private Context context;
        private String[] contents;
        public TitleAdapter(Context mContext, String[] contents) {
            this.context=mContext;
            this.contents=contents;
        }

        @Override
        public int getCount() {
            return contents.length;
        }

        @Override
        public String getItem(int position) {
            return contents[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null){
                convertView = LayoutInflater.from(context).inflate(R.layout.item_dialog_common, parent, false);
                holder=new Holder();
                holder.tv_item=convertView.findViewById(R.id.tv_item);
                convertView.setTag(holder);
            }else{
                holder= (Holder) convertView.getTag();
            }
            holder.tv_item.setText(getItem(position));
            return convertView;
        }
        class  Holder{
            private TextView tv_item;
        }
    }

    public interface SelectDialogOnItemClickListener{
        void selectDialogOnItemClick(int position);
    }
}
